home *** CD-ROM | disk | FTP | other *** search
/ Cre@te Online 2000 December / Cre@teOnline CD05.iso / MacSoft / XML Instance.sea / XML Instance / Required / ccs_util.jar / com / commerceone / util / net / URI.class (.txt) < prev   
Encoding:
Java Class File  |  1999-12-09  |  6.9 KB  |  344 lines

  1. package com.commerceone.util.net;
  2.  
  3. import java.io.Serializable;
  4. import java.net.URL;
  5.  
  6. public class URI implements Serializable {
  7.    String original;
  8.    String scheme;
  9.    String part;
  10.    byte isGeneric;
  11.    String authority;
  12.    String path;
  13.    String query;
  14.  
  15.    private void initGeneric() {
  16.       this.isGeneric = 1;
  17.       int index = this.part.indexOf(47, 2);
  18.       if (index >= 2) {
  19.          this.authority = this.part.substring(2, index);
  20.          int start = index;
  21.          index = this.part.indexOf(63, index);
  22.          if (index >= 0) {
  23.             this.path = this.part.substring(start, index);
  24.             this.query = this.part.substring(index + 1);
  25.          } else {
  26.             this.path = this.part.substring(start);
  27.          }
  28.       }
  29.  
  30.    }
  31.  
  32.    private void setGeneric(String scheme, String authority, String path, String query) {
  33.       this.isGeneric = 1;
  34.       this.scheme = scheme;
  35.       if (query == null) {
  36.          this.part = "//" + authority + path;
  37.       } else {
  38.          this.part = "//" + authority + path + '?' + query;
  39.       }
  40.  
  41.       this.authority = authority;
  42.       this.path = path;
  43.       this.query = query;
  44.       this.original = scheme + ':' + this.part;
  45.    }
  46.  
  47.    public String toString() {
  48.       return this.scheme + ':' + this.part;
  49.    }
  50.  
  51.    public URI(String uri) throws MalformedURIException {
  52.       this.createURI(uri);
  53.    }
  54.  
  55.    public URI(String scheme, String part) throws MalformedURIException {
  56.       this.original = scheme + ":" + part;
  57.       this.isGeneric = 0;
  58.       this.scheme = scheme;
  59.       this.part = part;
  60.       if (part.length() == 0) {
  61.          throw new MalformedURIException(this.original, "No scheme specific part has been specified.", 2);
  62.       } else {
  63.          this.authority = null;
  64.          this.path = null;
  65.          this.query = null;
  66.          if (part.startsWith("//")) {
  67.             this.isGeneric = -1;
  68.             if (part.length() == 2) {
  69.                throw new MalformedURIException(this.original, "No authority specified.", 3);
  70.             }
  71.          }
  72.  
  73.       }
  74.    }
  75.  
  76.    public URI(URI base, String uri) throws MalformedURIException {
  77.       this.original = uri;
  78.       if (uri.charAt(0) == '/' && uri.charAt(1) == '/') {
  79.          this.scheme = base.getDomainName();
  80.          this.isGeneric = -1;
  81.          this.part = uri;
  82.       } else {
  83.          int index = uri.indexOf(58);
  84.          if (index < 0) {
  85.             this.scheme = base.getDomainName();
  86.             if (!base.isGeneric()) {
  87.                throw new MalformedURIException(uri, "Cannot create a URI where base is non-generic and relative does not have an authority", 4);
  88.             }
  89.  
  90.             this.isGeneric = 1;
  91.             if (uri.charAt(0) == '/') {
  92.                if (uri.charAt(1) == '/') {
  93.                   int nextSlash = uri.indexOf(47, 2);
  94.                   if (nextSlash < 0) {
  95.                      throw new MalformedURIException(uri, "No schem specific part", 2);
  96.                   }
  97.  
  98.                   if (nextSlash < 3) {
  99.                      throw new MalformedURIException(uri, "No authority specified", 3);
  100.                   }
  101.  
  102.                   if (nextSlash >= uri.length() - 1) {
  103.                      throw new MalformedURIException(uri, "No scheme specific part", 2);
  104.                   }
  105.  
  106.                   this.authority = uri.substring(2, nextSlash);
  107.                   this.path = uri.substring(nextSlash);
  108.                } else {
  109.                   this.path = uri;
  110.                   this.authority = base.getAuthority();
  111.                }
  112.             } else {
  113.                this.authority = base.getAuthority();
  114.                this.path = base.getPath();
  115.                if (this.path.charAt(this.path.length() - 1) != '/') {
  116.                   int slash = this.path.lastIndexOf(47);
  117.                   if (slash > 0) {
  118.                      this.path = this.path.substring(0, slash + 1);
  119.                   } else {
  120.                      this.path = "/";
  121.                   }
  122.                }
  123.  
  124.                this.path = this.path + uri;
  125.                this.query = null;
  126.             }
  127.  
  128.             char[] pathArray = new char[this.path.length()];
  129.             this.path.getChars(0, this.path.length(), pathArray, 0);
  130.             StringBuffer newPath = new StringBuffer();
  131.             int i = 0;
  132.  
  133.             while(i < pathArray.length) {
  134.                if ((i == 0 || pathArray[i - 1] == '/') && pathArray[i] == '.' && i < pathArray.length - 1 && pathArray[i + 1] == '/') {
  135.                   i += 2;
  136.                } else {
  137.                   newPath.append(pathArray[i]);
  138.                   ++i;
  139.                }
  140.             }
  141.  
  142.             if (newPath.length() > 0 && newPath.charAt(newPath.length() - 1) == '.') {
  143.                newPath.setLength(newPath.length() - 1);
  144.             }
  145.  
  146.             this.path = newPath.toString();
  147.             i = 0;
  148.             int upDirStart = -1;
  149.  
  150.             while(!i && (upDirStart = this.path.indexOf("/../")) > 0) {
  151.                int lastSlash = this.path.lastIndexOf(47, upDirStart - 1);
  152.                if (lastSlash > -1) {
  153.                   this.path = this.path.substring(0, lastSlash + 1) + this.path.substring(upDirStart + 4);
  154.                }
  155.             }
  156.  
  157.             int quest = uri.indexOf(63);
  158.             if (quest < 0) {
  159.                this.part = "//" + this.authority + this.path;
  160.             } else {
  161.                this.query = uri.substring(quest + 1);
  162.                this.part = "//" + this.authority + this.path + '?' + this.query;
  163.             }
  164.          } else {
  165.             if (index == 0) {
  166.                throw new MalformedURIException(uri, "No scheme specified.", 1);
  167.             }
  168.  
  169.             this.scheme = uri.substring(0, index);
  170.             this.part = uri.substring(index + 1);
  171.             if (this.part.length() == 0) {
  172.                throw new MalformedURIException(uri, "No scheme specific part has been specified.", 2);
  173.             }
  174.  
  175.             this.authority = null;
  176.             this.path = null;
  177.             this.query = null;
  178.             if (this.part.startsWith("//")) {
  179.                this.isGeneric = -1;
  180.                if (this.part.length() == 2) {
  181.                   throw new MalformedURIException(uri, "No authority specified.", 3);
  182.                }
  183.  
  184.                int slashIndex = this.part.indexOf(47, 2);
  185.                if (slashIndex < 2) {
  186.                   throw new MalformedURIException(uri, "No scheme specific slash has been specified.", 2);
  187.                }
  188.             }
  189.          }
  190.       }
  191.  
  192.    }
  193.  
  194.    public URI(URI originalU) {
  195.       if (!originalU.isGeneric()) {
  196.          this.scheme = originalU.getDomainName();
  197.          this.part = originalU.getDomainIdentifier();
  198.          this.original = this.scheme + ':' + this.part;
  199.          this.isGeneric = 0;
  200.       } else {
  201.          this.setGeneric(originalU.getDomainName(), originalU.getAuthority(), originalU.getPath(), originalU.getQuery());
  202.       }
  203.  
  204.    }
  205.  
  206.    public URI(String scheme, String authority, String path, String query) {
  207.       if (path.charAt(0) != '/') {
  208.          path = path + "/";
  209.       }
  210.  
  211.       this.setGeneric(scheme, authority, path, query);
  212.    }
  213.  
  214.    public URI(URL from) {
  215.       this.scheme = from.getProtocol();
  216.       this.isGeneric = 1;
  217.       int port = from.getPort();
  218.       if (port != -1) {
  219.          this.authority = from.getHost() + ":" + port;
  220.       } else {
  221.          this.authority = from.getHost();
  222.       }
  223.  
  224.       String file = from.getFile();
  225.       if (file.charAt(0) != '/' && file.length() > 1) {
  226.          file = "/" + file;
  227.       }
  228.  
  229.       int queryIndex = file.indexOf(63);
  230.       if (queryIndex >= 0) {
  231.          this.query = file.substring(queryIndex + 1);
  232.          file = file.substring(0, queryIndex);
  233.       }
  234.  
  235.       String anchor = from.getRef();
  236.       if (anchor != null) {
  237.          file = file + "#" + anchor;
  238.       }
  239.  
  240.       this.path = file;
  241.       if (this.query == null) {
  242.          this.part = "//" + this.authority + this.path;
  243.       } else {
  244.          this.part = "//" + this.authority + this.path + '?' + this.query;
  245.       }
  246.  
  247.       this.original = this.scheme + ':' + this.part;
  248.    }
  249.  
  250.    public String getQuery() {
  251.       if (this.isGeneric < 0) {
  252.          this.initGeneric();
  253.       }
  254.  
  255.       return this.query;
  256.    }
  257.  
  258.    public int hashCode() {
  259.       return this.part.hashCode();
  260.    }
  261.  
  262.    public String getDomainIdentifier() {
  263.       return this.part;
  264.    }
  265.  
  266.    public boolean isGeneric() {
  267.       return this.isGeneric != 0;
  268.    }
  269.  
  270.    public boolean equals(Object obj) {
  271.       if (obj instanceof URI) {
  272.          URI compareTo = (URI)obj;
  273.          if (this.scheme.equals(compareTo.getDomainName()) && this.part.equals(compareTo.getDomainIdentifier())) {
  274.             return true;
  275.          }
  276.       }
  277.  
  278.       return false;
  279.    }
  280.  
  281.    public String getPath() {
  282.       if (this.isGeneric < 0) {
  283.          this.initGeneric();
  284.       }
  285.  
  286.       return this.path;
  287.    }
  288.  
  289.    public String getDomainName() {
  290.       return this.scheme;
  291.    }
  292.  
  293.    public String getOriginalURI() {
  294.       return this.original;
  295.    }
  296.  
  297.    public static boolean isAbsolute(String sysid) {
  298.       return sysid.indexOf(58) > 0;
  299.    }
  300.  
  301.    public String getAuthority() {
  302.       if (this.isGeneric < 0) {
  303.          this.initGeneric();
  304.       }
  305.  
  306.       return this.authority;
  307.    }
  308.  
  309.    protected void createURI(String uri) throws MalformedURIException {
  310.       int index = uri.indexOf(58);
  311.       if (index <= 0) {
  312.          throw new MalformedURIException(uri, "No scheme has been specified.", 1);
  313.       } else {
  314.          String newScheme = uri.substring(0, index);
  315.          String newPart = uri.substring(index + 1);
  316.          if (newPart.length() == 0) {
  317.             throw new MalformedURIException(uri, "No scheme specific part has been specified.", 2);
  318.          } else {
  319.             if (newPart.startsWith("//")) {
  320.                if (newPart.length() == 2) {
  321.                   throw new MalformedURIException(uri, "No authority specified.", 3);
  322.                }
  323.  
  324.                int slashIndex = newPart.indexOf(47, 2);
  325.                if (slashIndex < 2) {
  326.                   throw new MalformedURIException(uri, "No scheme specific slash has been specified.", 2);
  327.                }
  328.  
  329.                this.isGeneric = -1;
  330.             } else {
  331.                this.isGeneric = 0;
  332.             }
  333.  
  334.             this.original = uri;
  335.             this.scheme = newScheme;
  336.             this.part = newPart;
  337.             this.authority = null;
  338.             this.path = null;
  339.             this.query = null;
  340.          }
  341.       }
  342.    }
  343. }
  344.